package com.changeover.gwt.client.controller;

import com.changeover.gwt.client.event.LoginViewEvent;
import com.changeover.gwt.client.view.ILoginView;
import com.changeover.gwt.connection.dispatcher.shared.Dispatcher;
import com.changeover.gwt.shared.action.LoginAction;
import com.changeover.gwt.shared.model.LoginInfo;
import com.changeover.gwt.shared.result.LoginResult;
import com.changeover.gwt.shared.validation.LoginFieldVerifier;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

public class LoginController implements Controller {

    private LoginInfo entity;

    private final HandlerManager eventBus;
    private final ILoginView loginView;

    public LoginController(HandlerManager eventBus, ILoginView display) {
        this.eventBus = eventBus;
        this.loginView = display;
    }

    public void bind() {
        loginView.getLoginButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                login();
            }
        });
    }

    @Override
    public void go(HasWidgets container) {
        bind();
        container.clear();
        container.add(loginView.asWidget());
        initializeEntity();
    }

    private void initializeEntity() {
        loginView.clean();
        setEntity(new LoginInfo());
    }

    /**
     * Test method, it should be fetched from the server using the Dispatcher
     */
    private void login() {
        // validation
        String login = loginView.getLoginText();
        if (!LoginFieldVerifier.isValidLogin(login)) {
            loginView.setErrorText("Please enter at least four characters in to login field");
            loginView.clean();
            return;
        }
        String password = loginView.getPasswordText();
        if (!LoginFieldVerifier.isValidPassword(password)) {
            loginView.setErrorText("Please enter at least six characters in to password");
            loginView.clean();
            return;
        }

        // set entity in to action
        getEntity().setLogin(login);
        getEntity().setPassword(password);

        // sent request in to server
        Dispatcher.execute(new LoginAction(getEntity()), new AsyncCallback<LoginResult>() {

            @Override
            public void onFailure(Throwable caught) {
                GWT.log(caught.getLocalizedMessage());
                loginView.clean();
                loginView.setErrorText(caught.getMessage());
            }

            @Override
            public void onSuccess(LoginResult result) {
                loginView.setErrorText(result.getState());
                loginView.clean();
                eventBus.fireEvent(new LoginViewEvent());
            }

        });

    }

    public LoginInfo getEntity() {
        return entity;
    }

    public void setEntity(LoginInfo entity) {
        this.entity = entity;
    }

}
